#include "../include/Thread.hpp"
#include <stdio.h>
#include <string.h>
wd::Thread::Thread(CallBack &&cb)
:_pthid(0)
,_isRuning(false)
,_cb(std::move(cb))
{}

wd::Thread::~Thread(){}


void wd::Thread::start(){
    //创建线程
    if(!_isRuning){
       int ret = pthread_create(&_pthid,NULL,&start_rountin,this);

       //判断返回结果，线程是否创建成功
       if(ret != 0){
        //创建失败
        fprintf(stderr,"%s",strerror(ret));
       }


        _isRuning = true;
    }
}

void wd::Thread::join(){
    if(_isRuning){
        pthread_join(_pthid,NULL);
    }

}

void* wd::Thread::start_rountin(void * arg){
    Thread *ptask = static_cast<Thread*>(arg);
    //基类指针，指向继承它的对象MyThread
    if(ptask){
        ptask->_cb();
    }
    return NULL;
}
